In [1]:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import json

geojson = json.load(open('uk_regions_map.geojson','r'))
data = pd.read_csv('EU-referendum-result-data.csv')
In [2]:
data
Out[2]:
id Region_Code Region Area_Code Area Electorate ExpectedBallots VerifiedBallotPapers Pct_Turnout Votes_Cast ... Remain Leave Rejected_Ballots No_official_mark Voting_for_both_answers Writing_or_mark Unmarked_or_void Pct_Remain Pct_Leave Pct_Rejected
0 108 E12000006 East E06000031 Peterborough 120892 87474 87469 72.35 87469 ... 34176 53216 77 0 32 7 38 39.11 60.89 0.09
1 109 E12000006 East E06000032 Luton 127612 84633 84636 66.31 84616 ... 36708 47773 135 0 85 0 50 43.45 56.55 0.16
2 112 E12000006 East E06000033 Southend-on-Sea 128856 93948 93939 72.90 93939 ... 39348 54522 69 0 21 0 48 41.92 58.08 0.07
3 113 E12000006 East E06000034 Thurrock 109897 79969 79954 72.75 79950 ... 22151 57765 34 0 8 3 23 27.72 72.28 0.04
4 110 E12000006 East E06000055 Bedford 119530 86136 86136 72.06 86135 ... 41497 44569 69 0 26 1 42 48.22 51.78 0.08
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
377 65 E12000003 Yorkshire and The Humber E08000032 Bradford 342817 228729 228729 66.72 228727 ... 104575 123913 239 0 121 5 113 45.77 54.23 0.10
378 66 E12000003 Yorkshire and The Humber E08000033 Calderdale 149195 106005 106008 71.05 106004 ... 46950 58975 79 0 22 15 42 44.32 55.68 0.07
379 67 E12000003 Yorkshire and The Humber E08000034 Kirklees 307081 217460 217449 70.80 217428 ... 98485 118755 188 0 86 7 95 45.33 54.67 0.09
380 68 E12000003 Yorkshire and The Humber E08000035 Leeds 543033 387730 387730 71.39 387677 ... 194863 192474 340 39 116 8 177 50.31 49.69 0.09
381 69 E12000003 Yorkshire and The Humber E08000036 Wakefield 246096 175261 175259 71.17 175155 ... 58877 116165 113 0 46 4 63 33.64 66.36 0.06

382 rows × 21 columns

In [3]:
region = data.groupby(['Region'])['Votes_Cast','Remain','Leave','Rejected_Ballots'].sum()
region['Pct_Remain'] = region['Remain']/region['Votes_Cast']*100
region['Pct_Leave'] = region['Leave']/region['Votes_Cast']*100
region['Pct_Rejected'] = region['Rejected_Ballots']/region['Votes_Cast']*100
region.sort_values(by=['Votes_Cast'],inplace=True)
region['Name'] = region.index
region
Out[3]:
Votes_Cast Remain Leave Rejected_Ballots Pct_Remain Pct_Leave Pct_Rejected Name
Region
Northern Ireland 790523 440707 349442 374 55.748789 44.203900 0.047310 Northern Ireland
North East 1341387 562595 778103 689 41.941289 58.007346 0.051365 North East
Wales 1628054 772347 854572 1135 47.439888 52.490397 0.069715 Wales
East Midlands 2510496 1033036 1475479 1981 41.148681 58.772410 0.078909 East Midlands
Scotland 2681179 1661191 1018322 1666 61.957482 37.980381 0.062137 Scotland
Yorkshire and The Humber 2741172 1158298 1580937 1937 42.255575 57.673761 0.070663 Yorkshire and The Humber
West Midlands 2965369 1207175 1755687 2507 40.709099 59.206358 0.084543 West Midlands
South West 3174909 1503019 1669711 2179 47.340538 52.590830 0.068632 South West
East 3331312 1448616 1880367 2329 43.484849 56.445238 0.069912 East
North West 3668627 1699020 1966925 2682 46.312149 53.614745 0.073106 North West
London 3781204 2263519 1513232 4453 59.862388 40.019846 0.117767 London
South East 4963110 2391718 2567965 3427 48.189905 51.741045 0.069049 South East
In [4]:
fig = go.Figure()
l = ['Pct_Remain','Pct_Leave','Pct_Rejected']
for i in l:    
    fig.add_trace(go.Bar(
        x=region[i],
        y=region.index,
        orientation='h',
        name=i,
        opacity=0.8))
fig.update_layout(barmode='stack')
fig.show()
In [5]:
geojson['features'][0]['properties']
Out[5]:
{'name': 'North East',
 'cartodb_id': 1,
 'created_at': '2015-05-10T13:33:04Z',
 'updated_at': '2015-05-10T13:33:04Z'}
In [6]:
map_id={}
for feature in geojson['features']:
        feature['id'] = feature['properties']['cartodb_id']
        map_id[feature['properties']['name']] = feature['id']
        
region['id'] = region['Name'].apply(lambda x: map_id[x])
In [8]:
fig = px.choropleth(region,
                    locations='id',
                    geojson=geojson,
                    color='Pct_Leave',
                    hover_name='Name',
                    color_continuous_scale = px.colors.diverging.BrBG,
                    color_continuous_midpoint = 50
                   )
fig.update_geos(fitbounds='locations',visible=False)
fig.show()
In [ ]: